//
//  QQView.m
//  TestTableView
//
//  Created by Hunk on 12-1-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QQView.h"

@implementation QQView
@synthesize dataSource = _dataSource;
@synthesize delegate   = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        _sectionDictionary = [[NSMutableDictionary alloc] init];
        
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [self addSubview:_tableView];
    }
    return self;
}

- (void)setDataSource:(id<QQViewDataSource>)dataSource
{
    if(_dataSource != dataSource)
    {
        [_dataSource release];
        
        _dataSource = [dataSource retain];
    }
}

- (void)setDelegate:(id<QQViewDelegate>)delegate
{
    if(_delegate != delegate)
    {
        [_delegate release];
        
        _delegate = [delegate retain];
    }
}

#pragma mark - headerViewFoldInSection
- (BOOL)headerViewFoldInSection:(NSInteger)section
{
    return [[_sectionDictionary objectForKey:[NSString stringWithFormat:@"SectionKey_%d", section]] boolValue];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([_dataSource respondsToSelector:@selector(numberOfSectionsInQQView:)])
    {
        return [_dataSource numberOfSectionsInQQView:self];
    }
    
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BOOL bFlag = [[_sectionDictionary objectForKey:[NSString stringWithFormat:@"SectionKey_%d", section]] boolValue];
    
    if([_dataSource respondsToSelector:@selector(qqView:numberOfRowsInSection:flag:)])
    {
        return [_dataSource qqView:self numberOfRowsInSection:section flag:bFlag];
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_dataSource respondsToSelector:@selector(qqView:tableView:cellForRowAtIndexPath:)])
    {
        return [_dataSource qqView:self tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    return nil;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_delegate respondsToSelector:@selector(qqView:heightForRowAtIndexPath:)])
    {
        return [_delegate qqView:self heightForRowAtIndexPath:indexPath];
    }
    
    return 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if([_delegate respondsToSelector:@selector(qqView:heightForHeaderInSection:)])
    {
        return [_delegate qqView:self heightForHeaderInSection:section];
    }
    
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if([_dataSource respondsToSelector:@selector(qqView:viewForHeaderInSection:)])
    {
        UIView *headerView = [_dataSource qqView:self viewForHeaderInSection:section];
        
        // HeaderViewButton
        UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [headerButton setBackgroundColor:[UIColor clearColor]];
        [headerButton setFrame:headerView.bounds];
        [headerButton addTarget:self action:@selector(headerButtonHandle:) forControlEvents:UIControlEventTouchUpInside];
        [headerButton setTag:section];
        [headerView addSubview:headerButton];
        
        return headerView;
    }
    
    return nil;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//}

- (void)headerButtonHandle:(id)sender
{
    UIButton *headerButton = (UIButton *)sender;
    
    BOOL bFlag = [[_sectionDictionary objectForKey:[NSString stringWithFormat:@"SectionKey_%d", headerButton.tag]] boolValue];
    
    [_sectionDictionary setObject:[NSNumber numberWithBool:!bFlag] forKey:[NSString stringWithFormat:@"SectionKey_%d", headerButton.tag]];
    
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:headerButton.tag] 
              withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)dealloc
{
    [_tableView release];
    [_sectionDictionary release];
    [_dataSource release];
    [_delegate release];
    
    [super dealloc];
}

@end
